<template>
  <view v-if="visible" class="build-modal">
    <view class="modal-overlay" @click="$emit('close')"></view>
    <view class="modal-content">
      <view class="modal-header">
        <text class="modal-title">🏗️ 建筑管理</text>
        <button class="close-btn" @click="$emit('close')">×</button>
      </view>
      <view class="buildings-grid">
        <view 
          class="building-item" 
          v-for="(building, index) in buildings" 
          :key="index"
          :class="{ 
            'built': building.built, 
            'can-build': !building.built && canBuild(building),
            'cannot-build': !building.built && !canBuild(building)
          }"
          @click="$emit('build', building)"
        >
          <text class="building-icon">{{ building.icon }}</text>
          <text class="building-name">{{ building.name }}</text>
          <text class="building-level" v-if="building.built">Lv.{{ building.level }}{{ building.level < building.maxLevel ? '/升级' : '/最高' }}</text>
          <text class="building-status" v-else>{{ building.built ? '已建造' : '未建造' }}</text>
          <text class="building-description">{{ building.description }}</text>
          <text class="building-production">{{ building.production }}</text>
          <view class="building-cost" v-if="!building.built">
            <text class="cost-label">需要材料：</text>
            <text class="cost-items">{{ formatBuildingCost(building.cost) }}</text>
          </view>
          <view class="building-upgrade-cost" v-if="building.built && building.level < building.maxLevel && building.upgradeCost">
            <text class="cost-label">升级需要：</text>
            <text class="cost-items">{{ formatBuildingCost(building.upgradeCost) }}</text>
          </view>
          <view class="building-prerequisites" v-if="!building.built && building.prerequisites && building.prerequisites.length > 0">
            <text class="prereq-label">前置条件：</text>
            <text class="prereq-items">{{ formatPrerequisites(building.prerequisites) }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'BuildingModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    buildings: {
      type: Array,
      default: () => []
    },
    inventory: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    // 检查是否可以建造
    canBuild(building) {
      return this.checkBuildingPrerequisites(building) && this.checkBuildingResources(building).length === 0
    },
    
    // 检查建筑前置条件
    checkBuildingPrerequisites(building) {
      if (!building.prerequisites || building.prerequisites.length === 0) {
        return true
      }
      
      for (const prereqId of building.prerequisites) {
        const prereqBuilding = this.buildings.find(b => b.id === prereqId)
        if (!prereqBuilding || !prereqBuilding.built) {
          return false
        }
      }
      return true
    },
    
    // 检查建筑资源
    checkBuildingResources(building) {
      const missing = []
      for (const resource in building.cost) {
        if (this.inventory[resource] < building.cost[resource]) {
          missing.push(`${this.translateResource(resource)}×${building.cost[resource]}`)
        }
      }
      return missing
    },
    
    // 翻译资源名称
    translateResource(resource) {
      const map = {
        wood: '木材',
        stone: '石头',
        vine: '藤蔓',
        water: '淡水',
        metal: '金属',
        mud: '泥土',
        crystals: '水晶',
        herbs: '草药',
        mushrooms: '蘑菇',
        sand: '沙子',
        reeds: '芦苇',
        cactus: '仙人掌',
        shells: '贝壳'
      }
      return map[resource] || resource
    },
    
    // 格式化建筑成本
    formatBuildingCost(cost) {
      if (!cost) return '无'
      return Object.keys(cost).map(k => `${this.translateResource(k)}×${cost[k]}`).join('、')
    },
    
    // 格式化前置条件
    formatPrerequisites(prerequisites) {
      if (!prerequisites || prerequisites.length === 0) return '无'
      return prerequisites.map(id => {
        const building = this.buildings.find(b => b.id === id)
        return building ? building.name : id
      }).join('、')
    }
  }
}
</script>

<style scoped>
/* 弹窗样式 */
.build-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(5rpx);
}

.modal-content {
  position: relative;
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  border-radius: 20rpx;
  margin: 40rpx;
  width: calc(100% - 80rpx);
  height: calc(100% - 80rpx);
  max-width: 600rpx;
  max-height: 800rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1rpx solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.3);
}

.modal-header {
  padding: 30rpx;
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #f8fafc;
}

.close-btn {
  background: #ef4444;
  color: #fff;
  border: none;
  border-radius: 10rpx;
  padding: 8rpx 16rpx;
  font-size: 24rpx;
  cursor: pointer;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #dc2626;
  transform: scale(1.05);
}

/* 建筑网格样式 */
.buildings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300rpx, 1fr));
  gap: 20rpx;
  padding: 20rpx;
  max-height: 70vh;
  overflow-y: auto;
}

.building-item {
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  border: 2rpx solid #475569;
  border-radius: 16rpx;
  padding: 20rpx;
  text-align: left;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
  position: relative;
}

.building-item:hover {
  transform: translateY(-4rpx);
  box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.3);
  border-color: #64748b;
}

.building-item.built {
  background: linear-gradient(135deg, #065f46 0%, #047857 100%);
  border-color: #10b981;
  box-shadow: 0 4rpx 12rpx rgba(16, 185, 129, 0.3);
}

.building-item.can-build {
  border-color: #3b82f6;
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
  box-shadow: 0 4rpx 12rpx rgba(59, 130, 246, 0.3);
}

.building-item.cannot-build {
  opacity: 0.6;
  border-color: #ef4444;
  background: linear-gradient(135deg, #7f1d1d 0%, #ef4444 100%);
}

.building-icon {
  font-size: 48rpx;
  display: block;
  margin-bottom: 12rpx;
  text-align: center;
}

.building-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #f8fafc;
  display: block;
  margin-bottom: 8rpx;
  text-align: center;
}

.building-level {
  font-size: 24rpx;
  color: #10b981;
  font-weight: bold;
  display: block;
  margin-bottom: 8rpx;
  text-align: center;
}

.building-status {
  font-size: 24rpx;
  color: #94a3b8;
  display: block;
  text-align: center;
  margin-bottom: 8rpx;
}

.building-description {
  font-size: 22rpx;
  color: #cbd5e1;
  display: block;
  margin-bottom: 8rpx;
  line-height: 1.4;
}

.building-production {
  font-size: 20rpx;
  color: #10b981;
  display: block;
  margin-bottom: 12rpx;
  font-weight: 500;
}

.building-cost, .building-upgrade-cost {
  background: rgba(0, 0, 0, 0.3);
  border-radius: 8rpx;
  padding: 8rpx 12rpx;
  margin-bottom: 8rpx;
}

.cost-label {
  font-size: 20rpx;
  color: #fbbf24;
  font-weight: bold;
  display: block;
  margin-bottom: 4rpx;
}

.cost-items {
  font-size: 18rpx;
  color: #e5e7eb;
  display: block;
}

.building-prerequisites {
  background: rgba(239, 68, 68, 0.2);
  border: 1rpx solid rgba(239, 68, 68, 0.3);
  border-radius: 8rpx;
  padding: 8rpx 12rpx;
}

.prereq-label {
  font-size: 20rpx;
  color: #ef4444;
  font-weight: bold;
  display: block;
  margin-bottom: 4rpx;
}

.prereq-items {
  font-size: 18rpx;
  color: #fca5a5;
  display: block;
}
</style>
